# pip install python-docx

from docx import Document
from docx.shared import Inches  # 英寸


def first():
    doc1 = Document()
    doc1.add_heading('如何使用 Python 创建和操作 Word', 0)  # 添加标题
    doc1.save('../02.py_wordFile/01.创建简单word文档.docx')


def second():
    doc1 = Document()
    # 增加标题
    doc1.add_heading('如何使用 Python 创建和操作 Word', 0)  # 添加标题
    doc1.add_heading('一级标题', level=1)
    doc1.add_heading('二级标题', level=2)
    doc1.add_heading('三级标题', level=3)
    doc1.add_heading('四级标题', level=4)
    doc1.add_heading('五级标题', level=5)

    doc1.add_paragraph(
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~段落内容~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    doc1.add_paragraph('Title 段落内容~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', 'Title')  # 等于零级标题
    doc1.add_paragraph(
        'Python，翻译成汉语是蟒蛇的意思，并且Python的logo也是两条缠绕在一起的蟒蛇的样子，然而Python语言和蟒蛇实际上并没有一毛钱关系。')

    # 增加段落
    paragraph = doc1.add_paragraph(
        'Python，翻译成汉语是蟒蛇的意思，并且Python的logo也是两条缠绕在一起的蟒蛇的样子，然而Python语言和蟒蛇实际上并没有一毛钱关系。')
    paragraph.add_run('注意：这个是后加入的内容！！！')  # 增加段落

    # 增加无序列表
    doc1.add_paragraph('哪个不是动物？', style='List Bullet')
    doc1.add_paragraph('绵羊', style='List Bullet')
    doc1.add_paragraph('山羊', style='List Bullet')
    doc1.add_paragraph('小猪', style='List Bullet')
    doc1.add_paragraph('奶牛', style='List Bullet')
    doc1.add_paragraph('榴莲', style='List Bullet')
    doc1.add_paragraph('手机', style='List Bullet')

    # 增加有序列表
    doc1.add_paragraph('今年学习计划？', style='List Number')
    doc1.add_paragraph('Java', style='List Number')
    doc1.add_paragraph('Python', style='List Number')
    doc1.add_paragraph('JS', style='List Number')
    doc1.add_paragraph('Vue', style='List Number')
    doc1.add_paragraph('Flask', style='List Number')
    doc1.add_paragraph('Flink', style='List Number')

    # 增加引用
    doc1.add_paragraph('这是一个引用', style='Intense Quote')

    # 增加图片
    pic = doc1.add_picture('../02.py_wordFile/bak2.jpg', width=Inches(5))

    # 等比例增加图片
    pic2 = doc1.add_picture('../02.py_wordFile/bak2.jpg')
    height = pic.height  # 图片的高
    width = pic.width  # 图片的宽
    p_width = doc1.sections[0].page_width  # 获取文档的宽度
    sc = (p_width / 10 - doc1.sections[0].left_margin / 10 * 2) / (width / 10)
    pic2.height = int(height * sc)
    pic2.width = int(width * sc)

    # 增加表格
    table = doc1.add_table(rows=2, cols=3)
    cells = table.rows[0].cells
    cells1 = table.rows[1].cells
    cells[0].text = '编号'
    cells[1].text = '姓名'
    cells[2].text = '职业'
    cells1[0].text = '1'
    cells1[1].text = '张三'
    cells1[2].text = '搬运工'

    data = [
        [2, '吕布', '将军'],
        [3, '诸葛亮', '军师'],
        [4, '刘备', '主公'],
        [5, '曹操', '丞相']
    ]
    for i, name, work in data:
        cells = table.add_row().cells
        cells[0].text = str(i)
        cells[1].text = name
        cells[2].text = work

    doc1.save('../02.py_wordFile/0102.创建word文档.docx')


if __name__ == '__main__':
    # first()
    second()
